package joint.feature;

import java.util.Hashtable;
import java.util.List;

import abs.syn.Dependency;
import abs.syn.DependencyPath;
import abs.syn.Parse;
import def.BioNLPDocument;
import def.Event;

public class LinkDepFeature implements Feature {

	@Override
	public void apply(Hashtable<String, Integer> example, List<Event> events,
			BioNLPDocument doc, int sentId) {
		Parse parse = doc.parses[sentId];

		for (Event event: events){
			for (int i = 0; i < event.args.size(); i++){
				List<DependencyPath> list = parse.getDependencyPath(event.tspan.startIdx - doc.aText.sentStartPos[sentId], 
						event.tspan.endIdx - doc.aText.sentStartPos[sentId], 
						event.args.get(i).tspan.startIdx - doc.aText.sentStartPos[sentId], 
						event.args.get(i).tspan.endIdx - doc.aText.sentStartPos[sentId]);
				DependencyPath pp = null;
				if(!list.isEmpty()){
					int length = 100;
					for (DependencyPath dp: list){
						if (dp.deps.size() < length){
							length = dp.deps.size();
							pp = dp;
						}
					}
				}
				if (pp!=null){
					for (Dependency dep : pp.deps){
						String key = "EVENTTYPE_"+event.eventType+"_"+event.types.get(i)+"_"+dep.name;
						if (example.containsKey(key)){
							example.put(key,  example.get(key)+1);
						}else{
							example.put(key,  1);
						}
					}
				}
				else{
					System.err.println("[LinkDepFeature] cant find a path!!" + event+"   ARG:"+event.args.get(i));
				}
			}

		}
	}

}
